package com.dykj.fireFighting.web.costmanagement;

import java.util.*;
import java.util.stream.Collectors;

import com.dykj.fireFighting.api.fire.costmanagement.dto.CostManageMentDto;
import com.dykj.fireFighting.api.fire.costmanagement.entity.CostManagement;
import com.dykj.fireFighting.api.fire.costmanagement.service.ICostManagementService;
import com.dykj.fireFighting.api.fire.costmanagement.vo.CostManageMentVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;

/**
 * 成本管理接口
 *
 * @Author LIJIANJIE
 * @Date  2021-08-31 13:52:33
 */
@Slf4j
@Api(value = "成本管理接口", tags = {"成本管理接口"})
@RestController
@RequestMapping("/costManagement")
public class CostManagementController {

    @DubboReference
    private ICostManagementService costManagementService;

    /**
     * 获取单位id
     * @return String
     */
    public Long getEnterpriseId() {
        try {
            return ShiroUtil.getUser().getEnterpriseId();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 成本管理列表
     *
     * @return
     */
    @AutoLog(value = "成本管理-分页列表查询")
    @ApiOperation(value = "成本管理分页列表", notes = "成本管理分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<CostManagement>> treeGrid(CostManageMentDto commonDto) {
        if (commonDto.getWbId() == null) {
            if (getEnterpriseId() == null) {
                throw new BaseException("请登录系统");
            }
            commonDto.setWbId(getEnterpriseId());
        }
        PageVo<CostManagement> page = costManagementService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取成本管理信息
     */
    @AutoLog(value = "成本管理-获取成本管理信息")
    @ApiOperation(value = "获取成本管理信息", notes = "获取成本管理信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Integer", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<CostManagement> info(@PathVariable("id") String id){
        CostManagement costManagement = costManagementService.getById(id);
        return CommonResult.success(costManagement);
    }

    /**
     * 保存成本管理信息
     */
    @AutoLog(value = "成本管理-保存成本管理信息")
    @ApiOperation(value = "保存成本管理信息", notes = "保存成本管理信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody CostManagement costManagement) {
        try {
            costManagement.setId(UUID.randomUUID().toString());
            costManagement.setCreateTime(new Date());
            costManagement.setUpdateTime(new Date());
            if (getEnterpriseId() == null) {
                throw new BaseException("请登录系统");
            } else {
                costManagement.setWbId(getEnterpriseId());
                costManagement.setCreateBy(getName());
            }
            costManagementService.save(costManagement);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改成本管理信息
     */
    @AutoLog(value = "成本管理-修改成本管理信息")
    @ApiOperation(value = "修改成本管理信息", notes = "修改成本管理信息")
    //@RequiresPermissions("/costManagement/update")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody CostManagement costManagement) {
        try {
            costManagement.setUpdateTime(new Date());
            costManagement.setUpdateBy(getName());
            costManagementService.updateById(costManagement);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 成本统计
     */
    @AutoLog(value = "成本管理-成本统计")
    @ApiOperation(value = "成本管理-成本统计", notes = "成本管理-成本统计")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public CommonResult costCount(Integer year, Integer unitId) {
        List<CostManageMentVo> list = costManagementService.getCount(year,unitId,getEnterpriseId());
        return CommonResult.success(list);
    }

}
